/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package icontrole.core;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.util.ArrayList;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;

import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;

/**
 *
 * @author heliojr
 */
public final class Plot extends ChartPanel {

    ArrayList<XYSeries> _listSeries;

    public ArrayList<XYSeries> getListSeries() {
        return _listSeries;
    }

    public void setListSeries(ArrayList<XYSeries> _listSeries) {
        this._listSeries = _listSeries;
    }
    ArrayList<Color> _listColor;
    private JFreeChart chart;
    private XYPlot _plot;

    public XYPlot getPlot() {
        return _plot;
    }

    public void setPlot(XYPlot _plot) {
        this._plot = _plot;
    }
    private NumberAxis domain;
    private NumberAxis range;
    private XYLineAndShapeRenderer renderer;

    public Plot() {
        super(null);
        _listSeries = new ArrayList<XYSeries>();
        _listColor = new ArrayList<Color>();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
    }

    public void AddSeries(XYSeries name, Color cor) {
        _listSeries.add(name);
        _listColor.add(cor);
    }

    public void AddSpace(String x, String y) {
        domain = new NumberAxis(x);
        range = new NumberAxis(y);
        domain.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        range.setTickLabelFont(new Font("SansSerif", Font.PLAIN, 12));
        domain.setLabelFont(new Font("SansSerif", Font.PLAIN, 14));
        range.setLabelFont(new Font("SansSerif", Font.PLAIN, 14));
    }

    public void Loading(String name,boolean legend) {
        XYSeriesCollection dataset = new XYSeriesCollection();
        renderer = new XYLineAndShapeRenderer(true, false);

        for (int i = 0; i < _listSeries.size(); i++) {
            dataset.addSeries(_listSeries.get(i));
            renderer.setSeriesPaint(i, _listColor.get(i));
        }
        renderer.setStroke(new BasicStroke(3f, BasicStroke.JOIN_BEVEL, BasicStroke.CAP_BUTT));
        _plot = new XYPlot(dataset, domain, range, renderer);
        _plot.setBackgroundPaint(Color.lightGray);
        _plot.setDomainGridlinePaint(Color.white);
        _plot.setRangeGridlinePaint(Color.white);
        _plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        domain.setAutoRange(true);
        domain.setLowerMargin(50.0);
        domain.setUpperMargin(0.1);
        domain.setTickLabelsVisible(true);
        range.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        range.setAutoRange(true);
        chart = new JFreeChart(name, new Font("SansSerif", Font.BOLD, 14), _plot, legend);
        chart.setBackgroundPaint(Color.white);
        this.setChart(chart);
    }

    public void Clear() {
        for (XYSeries xYSeries : _listSeries) {
            xYSeries.clear();
        }
    }
}
